package com.hqz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqz.entity.Article;
import com.hqz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public Map getList(Integer page,Integer limit){

       Map<String,Object> map = new HashMap<String, Object>();
        //设置mybatisplus分页
       Page<Article> p = new Page<Article>();
//       p.setSize(limit); //设置每页记录数
//       p.setCurrent(page); //设置当前页码

        IPage<Article> iPage = articleService.selectList(p);

        map.put("msg","查询情况");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);

        return map;
    }

    /**
     * 增加
     * @param article
     * @param typeId
     * @return
     */
    @PostMapping("/list")
    public Map add(Article article,Integer typeId){

        Map<String,Object> result = new HashMap<String, Object>();

        result.put("state",articleService.add(article,typeId));
        return result;
    }

    /**
     * 删除
     * @param articleId
     * @return
     */
    @DeleteMapping("/article/del/{articleId}")
    public Map del(@PathVariable Integer articleId){
        Article article = new Article();
        article.setArticleId(articleId);
        article.setArticleName("");
        article.setIsDel(1);
        Map<String,Object> result = new HashMap<String, Object>();
        result.put("state",articleService.updateById(article));
        return result;
    }

    /**
     * 修改
     * @param article
     * @param typeId
     * @return
     */
    @PutMapping("/list")
    public Map edit(Article article,Integer typeId){
        Map<String,Object> result = new HashMap<String, Object>();

        result.put("state",articleService.update(article,typeId));
        return result;
    }
}
